package dao;

import java.util.ArrayList;
import java.util.List;

import javax.management.Query;

import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import vo.Role;
import vo.RoleUser;
import vo.Staff;

/**
 * @author bohan
 * @create 2017年5月22日
 */
public class StaffDaoImpl extends BaseDaoImpl<Staff> implements StaffDao{

	@Override
	public boolean deleteByNo(Integer staffNo) {
		String sql = "update staff set locked='true' where staffNo='"+staffNo+"'";
		excuteBySql(sql);
		System.out.println(sql);
		return true;
	}

	@Override
	public void addRoles(Staff staff,String roles) {
		Session session = getCurrentSession();
		Role role = new Role();
		String hql = "from Staff where staffNo='"+staff.getStaffNo()+"'";
		List list = new ArrayList();
		list = select(hql);
		staff = (Staff) list.get(0);
		if(roles.equals("admin")){
			String sql1 = "insert into role_user(userId,roleId) value ('"+staff.getId()+"','1')";
			excuteBySql(sql1);
		}else{
			String sql2 = "insert into role_user(userId,roleId) value ('"+staff.getId()+"','2')";
			excuteBySql(sql2);
		}
	}

	/* (non-Javadoc)
	 * @see dao.StaffDao#getRole(vo.Staff)
	 */
	@Override
	public String getRole(Staff staff) {
		String sql = "select * from role_user where userId = '"+staff.getId()+"'";
		List list = new ArrayList();
		list = getCurrentSession().createSQLQuery(sql).list();
		JSONArray json = new JSONArray();
		json = JSONArray.parseArray(JSON.toJSONString(list.get(0),SerializerFeature.DisableCircularReferenceDetect));
		return json.getString(2);
	}
	
	public boolean updateBySql(Staff staff){
		String sql =  "update staff set name='"+staff.getName()+"',telephone='"+staff.getTelephone()+"',"
				+ "password='"+staff.getPassword()+"',remark='"+staff.getRemark()+"' "
						+ "where id='"+staff.getId()+"'";
		try {
			System.out.println(sql);
			excuteBySql(sql);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
